<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户状态">
                <a-select v-model="queryParam.status" placeholder="请选择" default-value="0">
                  <a-select-option value="0">全部</a-select-option>
                  <a-select-option value="1">关闭</a-select-option>
                  <a-select-option value="2">运行中</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-input-search v-model="queryParam.search" placeholder="请输入关键字查询" enter-button="查询" @search="loadData"/>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!--头部操作区-->
      <div class="table-operator">
        <a-button v-action:add type="primary" icon="plus" @click="$refs.createModal.add()">新建</a-button>
        <a-button v-action-disabled:delete1 type="primary" icon="delete" @click="deleteById(selectedRowKeys)">删除</a-button>
        <a-button v-action-disabled:update type="primary" icon="lock" @click="disabledById(selectedRowKeys)">锁定</a-button>
      </div>

      <!--表格-->
      <a-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data-source="tableData"
        :rowSelection="rowSelection"
      >
        <!--状态插槽-->
        <span slot="status" slot-scope="text">
          <a-tag :color="text | statusFilter">{{ text }}</a-tag>
        </span>
        <!--图标-->
        <span slot="icons" slot-scope="text">
          <a-icon v-if="text !== ''" :type="text"/>
        </span>
        <!--权限插槽-->
        <span slot="actions" slot-scope="text, record">
          <a-tag v-for="(action, index) in record.actionData" :key="index" color="blue">{{ action.describe }}</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-action-disabled:info>详情</a>
          <a-divider type="vertical"/>
          <a v-action-visibility:update @click="disabledById(record)">禁用</a>
          <a-divider type="vertical"/>
          <a v-action:update @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a v-action:delete @click="deleteById(record.id)">删除</a>
        </span>
      </a-table>

      <!--新增/编辑弹窗-->
      <create-modal ref="createModal" @ok="handleSubmit"/>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import { getPermissions } from '@/api/manage'
import { deleteByIds } from '@/api/resource'
import CreateModal from '@/views/permission/modules/CreateModal'
import { STATUS_MAP } from '@/constants/commonConstant'
import { columns } from './constant'

export default {
  name: 'TableList',
  components: {
    STable,
    CreateModal
  },
  data() {
    return {
      // 表头
      columns,
      visible: false,
      // 查询参数
      queryParam: {},
      selectedRowKeys: [],
      selectedRows: [],
      rowSelection: {
        onChange: this.onSelectChange
      },
      tableData: []
    }
  },
  filters: {
    statusFilter(status) {
      return STATUS_MAP[status]
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      getPermissions(Object.assign({}, this.queryParam)).then(response => {
        if (response.success) {
          this.tableData = response.result
        }
      })
    },
    handleEdit(record) {
      this.$refs.createModal.edit(record)
    },
    // 根据ID删除
    deleteById(resourceIds) {
      if (!resourceIds || resourceIds.length === 0) {
        this.$message.error('请选择数据进行操作')
        return
      }
      deleteByIds(resourceIds).then(response => {
        if (response.success) {
          this.$refs.table.refresh(true)
          this.$message.success(response.message)
        } else {
          this.$message.error(response.message)
        }
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    disabledById(resourceIds) {
      if (!resourceIds || resourceIds.length === 0) {
        this.$message.error('请选择数据进行操作')
        return
      }
      this.$message.success(resourceIds)
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleSubmit() {
      // 新增/修改 成功时，重载列表
      this.loadData()
    }
  }
}
</script>
